package bl;

import java.math.BigInteger;

import javax.crypto.SecretKey;

public class IntegersTokenClassesManager implements TokenClassesManagerI {

	@Override
	public PublicParametersI CreatePublicParameters(int bitLength)
			throws Exception {
		return new IntegersPublicParameters(bitLength);
	}

	@Override
	public VEBTokensGeneratorI CreateVEBTokenGenerator(PublicParametersI ipp)
			throws IneligibleTypeException {
		if (!(ipp instanceof IntegersPublicParameters)) {
			throw new IneligibleTypeException();
		}

		return new IntegersVEBTokensGenerator((IntegersPublicParameters) ipp);
	}

	@Override
	public FriendClientDataI CreateFriendClientDataI(ClientDataI clientData,
			Object... objects) throws IneligibleTypeException {
		if (objects.length == 1 && clientData instanceof IntegersClientData
				&& objects[0] instanceof SecretKey) {
			return new IntegersFriendClientData((SecretKey) objects[0],
					(IntegersClientData) clientData);
		} else {
			throw new IneligibleTypeException();
		}
	}

	@Override
	public ClientDataI CreateClientDataI(Object... objects)
			throws IneligibleTypeException {
		if (objects.length == 1 && objects[0] instanceof BigInteger) {
			return new IntegersClientData((BigInteger) objects[0]);
		}
		if (objects.length == 2 && objects[0] instanceof BigInteger
				&& objects[0] instanceof BigInteger) {
			return new IntegersClientData((BigInteger) objects[0],
					(BigInteger) objects[1]);
		} else {
			throw new IneligibleTypeException();
		}
	}

	@Override
	public FriendClientDataI GenerateNewClient(PublicParametersI pp)
			throws IneligibleTypeException {
		if (!(pp instanceof IntegersPublicParameters)) {
			throw new IneligibleTypeException();
		}

		return new IntegersFriendClientData((IntegersPublicParameters) pp);
	}

	@Override
	public PublicParametersI CreatePublicParameters(byte[] ppBytes) throws Exception {
		return new IntegersPublicParameters(ppBytes);
	}
}
